﻿@model NameGroupModel
@{
    ViewData["Title"] = "任务历史管理";
}


<!-- Content Header (Page header) -->
<section class="content-header">
    <h1>
        任务执行历史
        <small></small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-map-marker"></i> 主页</a></li>
        <li><a href="#">任务执行历史</a></li>
        <li class="active">查询</li>
    </ol>
</section>


<!-- Main content -->
<section class="content">
    <!-- Default box -->
    <div class="box">
        <div class="box-header with-border">
            <h3 class="box-title">@(Model == null ? "所有" : "[" + Model.Group + "-" + Model.Name + "]") 执行历史</h3>
            <div class="box-tools pull-right">
                <button type="button" class="btn btn-box-tool" data-widget="collapse" data-toggle="tooltip"
                        title="Collapse">
                    <i class="fa fa-minus"></i>
                </button>
                <button type="button" class="btn btn-box-tool" data-widget="remove" data-toggle="tooltip" title="Remove">
                    <i class="fa fa-times"></i>
                </button>
            </div>
        </div>
        <div class="box-body">
            <!-- Main content -->
            <section class="content">
                <div class="row">
                    <div class="col-md-12">
                        <div class="box box-primary">
                            <div class="box-body">
                                <div class="row">
                                    <div class="col-md-12">
                                        <table class="table display dataTable" id="js_table">
                                            <thead>
                                                <tr>
                                                    <th style="text-align:center">任务组-名称</th>
                                                    <th style="text-align:center">触发器类型</th>
                                                    <th style="text-align:center">时间表达式</th>

                                                    <th style="text-align:center">开始时间</th>
                                                    <th style="text-align:center">结束时间</th>
                                                    <th style="text-align:center">程序集信息</th>
                                                    <th style="text-align:center">参数</th>
                                                    <th style="text-align:center">状态</th>
                                                    <th style="text-align:center">异常信息</th>
                                                    <th style="text-align:center">备注</th>
                                                    <th style="text-align:center">下次运行时间</th>
                                                    <th style="text-align:center">建立时间</th>
                                                </tr>
                                            </thead>
                                            <tbody></tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>

                        </div>
                        <div class="box-fooer">
                            <input id="refreshFlag" type="hidden" value="1" />
                            <button class="btn btn-primary" id="btnRefresh">暂停刷新</button>
                        </div>
                    </div>

                </div>
            </section>
        </div>
    </div>
</section>

@section Scripts{
    <script src="~/lib/momentjs/moment.min.js"></script>
    <script>




        $(document).ready(function () {

            var connection = new signalR.HubConnectionBuilder().withUrl("/RefreshTask").configureLogging(signalR.LogLevel.Information).build();

            connection.start().catch(err => console.error(err.toString()));


            $("#btnRefresh").click(function () {
                if ($("#refreshFlag").val() == "0") {
                    $("#refreshFlag").val("1");
                    $("#btnRefresh").text("暂停刷新");
                } else {
                    $("#refreshFlag").val("0");
                    $("#btnRefresh").text("恢复刷新");
                }

            });

            //渲染列表  页面初始化时
            var t = $("#js_table").DataTable({
                "processing": true,
                "searching": false,//关闭搜索框
                "serverSide": true,//服务器分页
                "bAutoWidth": false,
                "scrollX": true,
                "ajax": {
                    "url": "/TaskHistory/GetTaskHistories",
                    "type": "post",
                    "dataSrc": "list",//这里是后台返回的数据对象
                    "data": function (d) {//d 是原始的发送给服务器的数据，默认很长。
                        var param = {};//因为服务端排序，可以新建一个参数对象
                        param.start = d.start;//开始的序号
                        param.length = d.length;//要取的数据的条数
                        param.name ='@(Model==null?"":System.Web.HttpUtility.HtmlEncode(Model.Name))';
                        param.group='@(Model==null?"":System.Web.HttpUtility.HtmlEncode(Model.Group))';
                        return param;//自定义需要传递的参数。
                    }
                }, "createdRow": function (row, data, index) {
                    /* 设置表格中的内容居中 */
                    $('td', row).attr("class", "text-center");
                },
                //定义列: 取相应属性字段
                "columns": [

                    {
                        "data": "JobGroup",
                        "bSortable": false,
                        "render": function (data, type, full, callback) {
                            var params = data + "-" + full.JobName;
                            return params;
                        }
                    },
                    {
                        "data": "TriggerType",
                        "render": function (data, type, full, callback) {
                            switch (data) {
                                case 2: return "SIMPLE"; break;
                                case 1: return "CRON"; break;
                                default: return "UNKNOWN"; break;
                            }
                        }
                    },



                     {

                         "render": function (data, type, full, callback) {

                              switch (full.TriggerType) {
                                  case 2: return "<span title='重复次数:["+full.RepeatTimes+"]间隔:["+full.Interval+"]秒'>["+full.RepeatTimes+"]"+"-["+full.Interval+"]</span>"; break;
                                  case 1: return full.Cron; break;
                                default: return ""; break;
                            }


                        }
                    },




                    {
                        "data": "BeginTime",
                        "render": function (data, type, full, callback) {
                            return window.moment(data).format("YYYY-MM-DD HH:mm:ss");
                        }
                    },
                    {
                        "data": "EndTime",
                        "render": function (data, type, full, callback) {
                            if (data != null) {
                                return data != "0001-01-01T00:00:00" ? window.moment(data).format("YYYY-MM-DD HH:mm:ss") : null;
                            }
                            return data;
                        }
                    },
                    {
                        "data": "AssemblyName",
                        "render": function (data, type, full, callback) {
                                if (!data) return '';
                            var assembly= data + "." + full.ClassName;;


                            if (assembly && assembly.length > 10) {
                                assembly = "<span title='" + assembly + "'>" + data.substring(0, 10)+"..." + "</span>";
                            } else {
                                assembly = "<span title='" + assembly + "'>" + data + "</span>";
                            }

                            return assembly;
                        }
                    },
                    {
                        "data": "Params",
                        "render": function (data, type, full, callback) {
                                if (!data) return '';
                            var params = "";
                            if (data && data.length > 10) {
                                params = "<span title='" + data + "'>" + data.substring(0, 10)+"..." + "</span>";
                            } else {
                                params = "<span title='" + data + "'>" + data + "</span>";
                            }

                            return params;
                        }
                    },
                    {
                        "data": "JobStatus",
                        "render": function (data, type, full, callback) {

                            switch (data) {
                                case 0: return "未启动"; break;
                                case 1: return "运行中"; break;
                                case 2: return "完成"; break;
                                case 3: return "完成但有异常"; break;
                                default: return "未知"; break;
                            }
                        }
                    },
                    {
                        "data": "Exception",
                        "render": function (data, type, full, callback) {
                                if (!data) return '';
                            var ex_info = "";

                            if (data && data.length > 15) {
                                ex_info = "<span title='" + data.replace(/\'/g, "")  + "'>" + data.substring(0, 15)+"..." + "</span>";
                            } else {
                                ex_info = "<span title='" + data + "'>" + data + "</span>";
                            }

                            return ex_info;
                        }
                    },
                    {
                        "data": "Notes",
                        "render": function (data, type, full, callback) {
                            var notes = "";

                            if (!data) return '';

                            if (data && data.length > 10) {
                                notes = "<span title='" + data + "'>" + data.substring(0, 10)+"..." + "</span>";
                            } else {
                                notes = "<span title='" + data + "'>" + data + "</span>";
                            }

                            return notes;
                        }
                    },
                    {
                        "data": "NextFireTime",
                        "render": function (data, type, full, callback) {
                            if (data) {
                                return window.moment(data).format("YYYY-MM-DD HH:mm:ss");
                            } else {
                                return "";
                            }
                        }
                    },
                    {
                        "data": "CreatedTime",
                        "render": function (data, type, full, callback) {
                            if (data) {
                                return window.moment(data).format("YYYY-MM-DD HH:mm:ss");
                            } else {
                                return "";
                            }
                        }
                    },
                ],
                "language": {
                    "lengthMenu": "每页 _MENU_ 条记录",
                    "zeroRecords": " ",
                    "info": "当前 _START_ 条到 _END_ 条 共 _TOTAL_ 条",
                    "infoEmpty": "无记录",
                    "infoFiltered": "(从 _MAX_ 条记录过滤)",
                    "search": "全文搜索",
                    "processing": "载入中",
                    "paginate": {
                        "first": "首页",
                        "previous": "上一页",
                        "next": "下一页",
                        "last": "尾页"
                    }
                },
                "aLengthMenu": [
                    [25, 50, 100,300], [25, 50, 100,300]
                ],
                "paging": true,//分页
                "pagingType": "full_numbers",//显示首页尾页
                "ordering": false,//排序
                "info": true,//信息
                "initComplete": function (settings, data) {

                },
                "drawCallback": function (settings, data) {

                }

            });

            connection.on("ReceiveMessage", (user, message) => {
                console.log("---------------", message);
                if (message == "RefreshTask" && $("#refreshFlag").val() == "1") {
                    t.draw();
                }

            });
        });

    </script>
}
